package com.chinacoal.microservice.service.impl;

import com.auth0.jwt.JWT;
import com.baomidou.mybatisplus.plugins.Page;
import com.chinacoal.microservice.mapper.CcmsSmartleasingWorkOrderMapper;
import com.chinacoal.microservice.pojo.CcmsSmartleasingWorkOrder;
import com.chinacoal.microservice.pojo.Currency;
import com.chinacoal.microservice.service.WorkOrderService;
import com.chinacoal.microservice.util.QueryCriteria;
import com.chinacoal.microservice.util.Result;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author zhuliang
 * @version V1.0
 * @Description: 运维管理-工单-后端实现类
 * @date 2020-12-16 17：55
 */
@Service
public class WorkOrderServiceImpl implements WorkOrderService {

    @Autowired
    private CcmsSmartleasingWorkOrderMapper workOrderMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * @Description: 查询所有的工单信息（带分页）
     * @author zhuliang
     * @date 2020/12/16 18:02
     */
    @Override
    public Result<List<CcmsSmartleasingWorkOrder>> getAllByPage(QueryCriteria queryCriteria, String token) {
        //得到登录人信息
        Currency currency = (Currency) redisTemplate.opsForValue().get("ccms:" + JWT.decode(token).getSignature());
        if (null == currency) {
            currency = (Currency) redisTemplate.opsForValue().get("ccms:" + JWT.decode(token).getClaim("phone").asString());
        }

        //设置分页条件
        Page<CcmsSmartleasingWorkOrder> toPgae = new Page<>(queryCriteria.getPage(), queryCriteria.getPageSize());

        //分页查询工单信息
        List<CcmsSmartleasingWorkOrder> workOrders = workOrderMapper.getAllByPage(toPgae,
                                                                                    currency.getProperty(),
                                                                                    currency.getManagementOffice());

        return Result.success(workOrders, toPgae.getTotal());
    }

    /**
     * @Description: 条件查询所有的工单信息（带分页）
     * @author zhuliang
     * @date 2020/12/16 18:28
     */
    @Override
    public Result<List<CcmsSmartleasingWorkOrder>> getAllByCondition(QueryCriteria queryCriteria, String token) {
        //得到登录人信息
        Currency currency = (Currency) redisTemplate.opsForValue().get("ccms:" + JWT.decode(token).getSignature());
        if (null == currency) {
            currency = (Currency) redisTemplate.opsForValue().get("ccms:" + JWT.decode(token).getClaim("phone").asString());
        }

        //设置分页条件
        Page<CcmsSmartleasingWorkOrder> toPgae = new Page<>(queryCriteria.getPage(), queryCriteria.getPageSize());

        //条件查询工单信息
        List<CcmsSmartleasingWorkOrder> workOrders = workOrderMapper.getAllByCondition(queryCriteria.getWorkOrderStatus(),
                                                                                        queryCriteria.getFkWorkOrderTypeId(),
                                                                                        currency.getProperty(),
                                                                                        currency.getManagementOffice(),
                                                                                        toPgae);

        return Result.success(workOrders, toPgae.getTotal());
    }

    /**
     * @Description: 处理工单信息
     * @author zhuliang
     * @date 2020/12/16 18:38
     */
    @Override
    public Result handleByWorkOrder(CcmsSmartleasingWorkOrder workOrder, String token) {
        //得到登录人id
        Currency currency = (Currency) redisTemplate.opsForValue().get("ccms:" + JWT.decode(token).getSignature());
        if (null == currency) {
            currency = (Currency) redisTemplate.opsForValue().get("ccms:" + JWT.decode(token).getClaim("phone").asString());
        }

        //修改工单信息
        workOrder.setWorkOrderStatus("1");
        workOrder.setLastUpdateDate(new Date());
        workOrder.setLastUpdatedBy(currency.getLoginId());
        workOrder.setHandler(currency.getLoginName());

        //修改工单信息
        workOrderMapper.updateById(workOrder);

        return Result.success();
    }

    /**
     * @Description: 根据工单id查询工单信息
     * @author zhuliang
     * @date 2020/12/16 18:38
     */
    @Override
    public Result<CcmsSmartleasingWorkOrder> getWorkOrderById(String pkWorkOrderId) {
        //根据工单id查询信息
        CcmsSmartleasingWorkOrder workOrder = workOrderMapper.getWorkOrderById(pkWorkOrderId);

        //拆分地址
        if (null != workOrder && StringUtils.isNotBlank(workOrder.getScenePicture())) {
            String[] split = workOrder.getScenePicture().split(",");
            workOrder.setScenePictureList(split);
        }

        return Result.success(workOrder);
    }

}
